# -*- coding: utf-8 -*-

#    Copyright (C) 2005-2014 by Promotux
#                        di Francesco Meloni snc - http://www.promotux.it/

#    Author: Francesco Meloni  <francesco@promotux.it>

#    This file is part of OdmCollect. http://www.promogest.me

#    OdmCollect is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.

#    OdmCollect is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with OdmCollect.  If not, see <http://www.gnu.org/licenses/>.

import sys
from web_lib.page import Page
from web_lib.webutils import *
import Environment
from sqlalchemy.orm import clear_mappers
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.exc import *
from sqlalchemy.engine import reflection
from sqlalchemy.event import listen
from sqlalchemy.pool import NullPool
from sqlalchemy.schema import CreateSchema
from web_lib.webutils import leggiInSessione


@expose('/pg/main')
@expose('/main')
@expose('/pg')
def mainpage(req, subdomain=None):
    if not getUserFromId(req) and not leggiInSessione(sezione="Main", chiave="id_oper"):
        redirectUrl="/"
        return Page(req).redirect(redirectUrl)
    if leggiInSessione(sezione="Main", chiave="schema"):
        from dao.News import News
        news = News().select(BatchSize=None)
    else:
        news=[]
    pageData = {'file': "/pg/mainpage",
                "newss": news,
                }
    return Page(req).render(pageData)

@expose('/pg/cs')
def cschema(req):
    sch = req.form.get("schema")
    change_schema(req,sch)
    return Response("OK")
